﻿using System;
using System.Windows.Markup;
using System.Xaml;

namespace Microsoft.Services.Clab.UI.Mvvmc.Presentation.Markup
{
    [MarkupExtensionReturnType(typeof(ActionCommand))]
    public class ActionCommandExtension : MarkupExtension
    {
        public string ActionName { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (serviceProvider != null)
            {
                var rootObjectProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
                if (rootObjectProvider != null)
                {
                    return new ActionCommand(this.ActionName, rootObjectProvider.RootObject);
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
}